﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using IPC.Communication.Core;
using IPC.Communication.ModBus;

namespace IPC.Communication.Profinet.Delta
{
    public class DeltaDvpTcpNet : ModbusTcpNet
    {
        public DeltaDvpTcpNet()
        {
            base.ByteTransform.DataFormat = DataFormat.CDAB;
        }

        public DeltaDvpTcpNet(string ipAddress, int port = 502, byte station = 1)
            : base(ipAddress, port, station)
        {
            base.ByteTransform.DataFormat = DataFormat.CDAB;
        }

        public override OperateResult<string> TranslateToModbusAddress(string address, byte modbusCode)
        {
            return DeltaHelper.PraseDeltaDvpAddress(address, modbusCode);
        }

        public override OperateResult<bool[]> ReadBool(string address, ushort length)
        {
            return DeltaHelper.ReadBool(base.ReadBool, address, length);
        }

        public override OperateResult Write(string address, bool[] values)
        {
            return DeltaHelper.Write(base.Write, address, values);
        }

        public override OperateResult<byte[]> Read(string address, ushort length)
        {
            return DeltaHelper.Read(base.Read, address, length);
        }

        public override OperateResult Write(string address, byte[] value)
        {
            return DeltaHelper.Write(base.Write, address, value);
        }

        //[AsyncStateMachine(typeof(<ReadBoolAsync>d__7))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
        //{
        //    <ReadBoolAsync>d__7 stateMachine = new <ReadBoolAsync>d__7();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__8))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, bool[] values)
        //{
        //    <WriteAsync>d__8 stateMachine = new <WriteAsync>d__8();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadAsync>d__9))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
        //{
        //    <ReadAsync>d__9 stateMachine = new <ReadAsync>d__9();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__10))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, byte[] value)
        //{
        //    <WriteAsync>d__10 stateMachine = new <WriteAsync>d__10();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        public override string ToString()
        {
            return String.Format("DeltaDvpTcpNet[{0}:{1}]", IpAddress, Port);
        }
    }
}
